<?php
/**
 * This file is part of DomBankLink.
 * 
 * DomBankLink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomBankLink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomBankLink.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
 * @package DomBankLink
 */
abstract class DomBankLinkObject extends DomArObject {
	
/**
	* @orm plugin DomArDateTime()
	*/
	public $sendTime;
	/**
	* @orm char(15)
	*/
	public $sendIp;
	
	/**
	* @orm plugin DomArDateTime()
	*/
	public $backTime;
	/**
	* @orm char(15)
	*/
	public $backIp;
	
	/**
	* @orm enum({sentToBank,ok,cancel,error,pending}) index
	*/
	public $status = 0;
	/**
	 * @orm bool
	 */
	public $handled = false;
	/**
	* @orm char(10)
	*/
	public $bank;
	/**
	 * @orm char(10) index
	 */
	public $profile;
	/**
	 * @orm char(255)
	 * @orm validator method callBackUrlValidator
	 */
	public $callbackUrl;
	
	const OK = 'ok';
	const CANCEL = 'cancel';
	const ERROR = 'error';
	const SENT_TO_BANK = 'sentToBank';
	const PENDING = 'pending';
	
	/**
	 * Creates new payment object
	 *
	 * @param Float $price
	 * @param String $description
	 * @param String $profile
	 * @return BankPayment
	 */
	public static function createNew( $profile ) {
		$class = get_called_class();
		$payment = new $class();
		$payment->profile = (string)strtolower($profile);
		
		$link = DomBankLink::getLink( $payment->profile );
		if( !$link ) throw new  RuntimeException('BankLink profile "'.$profile.'" doesn\'t exist!');
		$payment->bank = $link->getKey();
		
		$payment->sendTime = 'now';
		$payment->sendIp = ip();
		
		return $payment;
	}
	/**
	 * Sends user to bank.
	 * Ends script execution!
	 */
	public function sendToBank() {
		$this->status = self::SENT_TO_BANK;
		if( $this->isChanged() && !$this->save() )
			throw new RuntimeException( $this->getValidationMessages('<br>') );
		
		DomBankLink::getLink( $this->profile )->sendToBank( $this );
	}
	/**
	 * Handles bank callback
	 *
	 * @param String $profile
	 * @return DomBankPayment
	 */
	public static function handleReplay( $profile, $requestId = NULL ) {
		$paymentClassName = get_called_class();
		$link = DomBankLink::getLink( $profile );
		
		if( !$link )
			throw new RuntimeException('Link with profile name "'.$profile.'" can\'t be found!');
			
		$payment = $link->handleReplay( $paymentClassName, $requestId );
		
		return $payment;
	}
	/**
	 * Sets payment status to finished. Be sure to call this when payment is handled!
	 */
	public function finished() {
		$this->handled = true;
		$this->save();
	}
	
	public function callBackUrlValidator( $url ) {
		if( strpos($url, '?') > 0 or strpos($url,'&') )
			return 1001;
			
		return 0;
	}
	
	
}
?>